<?php
class Brand extends AppModel{
    public $useTable  = 'brand';
    public $lang_code = 'vn';
    
    

    function add($data = array())
    {
        $this->create();
        $this->save($data);
		return $this->getLastInsertId();
    }
    
    function get_brand()
    {
        $lang_code = $this->lang_code;
        $sql = array(
            "conditions"=> array(
				"lang_code ="=> "$lang_code",
			),
			"order" => array('sort_order' => 'asc'),
			);
        $data = $this->find("all",$sql);
		return $data;
    }
    
    public function get_brand_by_id($brand_id = "", $lang = "")
    {
    	if($brand_id == "")
    		return null;
    	$lang_code = $this->lang_code;

    	$where_lang = "AND brand.lang_code = '$lang_code'";
    	if($lang == 'all')
    		$where_lang = "";

    	$sql = "SELECT * FROM brand WHERE brand_id = '$brand_id'";
    	$data = $this->query($sql);
        $result = array();
        foreach ($data as $value) {
			$lang_code = $value['brand']['lang_code'];
			$result[$lang_code] = $value['brand'];

		}
		return $result;
    }
    
    function update($brand_id = null, $lang_code = null, $data = array())
    {
		return $this->updateAll($data, array('brand_id' => $brand_id, 'lang_code' => $lang_code));
    }
    
    function get_brand_by_group($group = null)
    {
        $lang_code = $this->lang_code;
        $sql = array(
            "conditions"=> array(
                'group' => $group,
                'status' => 1,
			),
			"order" => array('sort_order' => 'asc'),
			);
        $data = $this->find("all",$sql);
        $result = array();
		foreach($data as $value)
        {
            $lang_code = $value['Brand']['lang_code'];
			$result[$lang_code][] = $value['Brand'];
        }
        return $result;
    }
    
    function del($brand_id = null)
    {
       $sql = "DELETE FROM brand WHERE brand_id = '$brand_id'";
   	    return $this->query($sql);
    }
    
    function change_status($brand_id = "")
    {
    	$sql = "SELECT * FROM brand WHERE brand_id = '$brand_id'";
        $data = $this->query($sql);
        if(!empty($data))
        {
            $status = $data[0]['brand']['status'];
            $change_status = $status == 1 ? '0' : '1';
            $sql = "UPDATE brand SET status = '$change_status' WHERE brand_id = '$brand_id'";
            $this->query($sql);
            return $change_status;
        }
        return -1;
    }
}